<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div{
            width: 100px;
            height: 100px;
            background-color: pink;
            position: absolute;
        }
        span{
            width: 200px;
            height: 200px;
            background-color: purple;
            position: absolute;
            left: 0;
            top: 200px;
        }
    </style>
</head>
<body>
    <button>触发动画</button>
    <div></div>
    <span></span>
    <script>
        var obj = [];
        var div = document.querySelector('div');
        var span = document.querySelector('span');
        function animate(obj,target){
            //不断点击按钮，元素定时器会叠加
            //解决方案：只有一个定时器
            clearInterval(obj.timer);
            obj.timer = setInterval(function(){
            if(obj.offsetLeft >= target){
                clearInterval(obj.timer);
            }else{
                obj.style.left =  obj.offsetLeft + 1 + 'px';
                console.log(obj.style.left);
            }
        },10);
        }
        animate(div,500);
        var btn = document.querySelector('button');
        btn.addEventListener('click',function(){
            animate(span,600);
        });
    </script>
</body>
</html>